﻿var com = com || {};
com.viewModel = com.viewModel || {};

com.viewModel.edit = function ( data ) {
    var me = this;
    me.urls = data.urls;
    me.dataSource = data.dataSource;
    me.form = ko.mapping.fromJS( data.form || data.defaultForm );
    me.setting = data.setting;
    me.resx = data.resx;
    me.httpMethod = data.form ? "PUT" : "POST";
    me.isEditing = me.httpMethod == "PUT";
    delete me.form.__ko_mapping__;

    me.save = function () {
        com.ajax( {
            type: me.httpMethod,
            url: me.urls.edit,
            data: ko.toJSON( me.form ),
            success: function ( d ) {
                com.message( "success", me.resx.saveSuccess );
                me.httpMethod = "PUT";
                me.isEditing = true;
            }
        } );
    };
    me.audit = function () {
        console.log( "audit" );
    };
    me.close = function () {
        console.log( "close" );
        com.close();
    };
    me.isEnabled = function ( field ) {
        if ( me.isEditing && me.setting && me.setting.disabledFieldsInEdit ) {
            return $.inArray( field, me.setting.disabledFieldsInEdit ) < 0;
        } else {
            return true;
        }
    };

}